package com.auction.system.action.auction;

import com.auction.system.base.BaseResponse;
import com.auction.system.constant.SystemConstant;
import com.auction.system.domain.dto.auction.AuctionGood;
import com.auction.system.domain.query.auction.AuctionGoodQuery;
import com.auction.system.enums.*;
import com.auction.system.service.auction.IAuctionGoodService;
import com.auction.system.utils.DateUtil;
import com.auction.system.utils.StringUtil;
import com.auction.system.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * @ClassName AuctionGoodAction
 * @Author 王伟学
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/auctionGood")
public class AuctionGoodAction {

    @Autowired
    private IAuctionGoodService auctionGoodService;

    /**
     * 进入拍品信息审核列表页面
     * @return
     */
    @RequestMapping(value = "/auditList",method = RequestMethod.GET)
    public String auditList(){
        return "auctionGood/auditList";
    }

    /**
     * 进入拍品信息列表页面(我发布的拍品)
     * @return
     */
    @RequestMapping(value = "/sellerList",method = RequestMethod.GET)
    public ModelAndView sellerList(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("bidUserId", UserUtil.getUserId());
        mav.setViewName("auctionGood/sellerList");
        return mav;
    }

    /**
     * 我的竞价记录
     * @return
     */
    @RequestMapping(value = "/buyerList",method = RequestMethod.GET)
    public ModelAndView buyerList(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("maxPriceUserId", UserUtil.getUserId());
        mav.setViewName("auctionGood/buyerList");
        return mav;
    }

    /**
     * 进入拍品信息列表页面(所有拍品)
     * @return
     */
    @RequestMapping(value = "/allList",method = RequestMethod.GET)
    public String allList(){
        return "auctionGood/allList";
    }

    /**
     * 获取拍品信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public AuctionGoodQuery getList(AuctionGoodQuery query) {
        try {
            //排序规则,按最新上架时间排序(默认按最新上架时间排序)
            if(StringUtil.isEmpty(query.getSortType()) || EnumSortType.TIME_SORT.getCode().equals(query.getSortType())){
                query.setOrderField("createTime");
                query.setOrderType(EnumSqlOrder.DESC_ORDER.getCode());
            }
            //按竞拍次数排序
            if(EnumSortType.NUMM_SORT.getCode().equals(query.getSortType())){
                query.setOrderField("bidNum");
                query.setOrderType(EnumSqlOrder.DESC_ORDER.getCode());
            }
            query = auctionGoodService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取拍品信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取拍品信息数据异常");
        }
        return query;
    }

    /**
     * 获取拍品信息数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSellList",method = RequestMethod.POST)
    public AuctionGoodQuery getSellList(AuctionGoodQuery query) {
        try {
            query.setBidUserId(Long.parseLong(UserUtil.getUserId()));
            query = auctionGoodService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取拍品信息列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取拍品信息数据异常");
        }
        return query;
    }

    /**
     * 进入拍品发布页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView preAdd(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("bidUserId", UserUtil.getUserId());
        mav.setViewName("auctionGood/add");
        return mav;
    }

    /**
     * 发布拍品
     * @param auctionGood
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated AuctionGood auctionGood, BindingResult result){
        BaseResponse response = new BaseResponse();
        try{
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("AuctionGood-add参数有误");
                log.info("AuctionGood-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            //前台时间格式转换
            auctionGood.setEndTime(DateUtil.StringToDate(auctionGood.getEndTimeStr(), EnumDateStyle.YYYY_MM_DD_HH_MM_SS));
            //校验拍品的结束时间是否早于当前时间
            if(!auctionGood.getEndTime().after(new Date())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("拍品的结束时间不允许早于当前时间");
                return response;
            }
            //拍品信息的状态为待审核
            auctionGood.setBidNum(0);
            auctionGood.setAuctionStatus(EnumAuctionStatus.TO_AUDIT.getCode());
            auctionGoodService.add(auctionGood);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("新增拍品信息成功");
        }catch (Exception e){
            log.error("新增拍品信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("新增拍品信息失败");
        }
        return response;
    }

    /**
     * 进入修改页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView preEdit(Long id){
        ModelAndView mav = new ModelAndView();
        try{
            AuctionGood auctionGood = auctionGoodService.get(id);
            mav.addObject("auctionGood",auctionGood);
            mav.setViewName("auctionGood/edit");
        }catch (Exception e){
            log.error("获取拍品信息详情失败",e);
            mav.addObject(SystemConstant.ERROR_KEY,"系统异常");
            mav.setViewName("auctionGood/sellerList");
        }
        return mav;
    }

    /**
     * 修改拍品信息
     * @param auctionGood
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public BaseResponse edit(@Validated AuctionGood auctionGood, BindingResult result){
        BaseResponse response = new BaseResponse();
        try{
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("AuctionGood-edit参数有误");
                log.info("AuctionGood-edit参数有误："+result.getAllErrors().toString());
                return response;
            }
            AuctionGood auctionGoodTemp = auctionGoodService.get(auctionGood.getId());
            //待审核状态的拍品才允许修改
            if(!EnumAuctionStatus.TO_AUDIT.getCode().equals(auctionGoodTemp.getAuctionStatus())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("待审核状态的拍品才允许修改");
                return response;
            }
            //前台时间格式转换
            auctionGood.setEndTime(DateUtil.StringToDate(auctionGood.getEndTimeStr(), EnumDateStyle.YYYY_MM_DD_HH_MM_SS));
            //校验拍品的结束时间是否早于当前时间
            if(!auctionGood.getEndTime().after(new Date())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("拍品的结束时间不允许早于当前时间");
                return response;
            }
            auctionGoodService.edit(auctionGood);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("修改拍品信息成功");
        }catch (Exception e){
            log.error("修改拍品信息失败",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("修改拍品信息失败");
        }
        return response;
    }

    /**
     * 删除拍品信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public BaseResponse delete(Long id){
        BaseResponse response = new BaseResponse();
        try{
            AuctionGood auctionGood = auctionGoodService.get(id);
            //竞拍成功的拍品信息不允许删除
            if(EnumAuctionStatus.BID_SUCCESS.getCode().equals(auctionGood.getAuctionStatus())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("竞拍成功的拍品信息不允许删除");
                return response;
            }
            auctionGoodService.delete(id);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("删除拍品信息成功");
        }catch (Exception e){
            log.error("删除拍品信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("删除拍品信息失败");
        }
        return response;
    }

    /**
     * 进入详情页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/view",method = RequestMethod.GET)
    public ModelAndView view(Long id){
        ModelAndView mav = new ModelAndView();
        AuctionGood auctionGood = auctionGoodService.get(id);
        mav.addObject("auctionGood",auctionGood);
        mav.setViewName("auctionGood/view");
        return mav;
    }

    /**
     * 审核拍品
     * @param id
     * @param auditStatus
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    public BaseResponse audit(Long id, String auditStatus){
        BaseResponse response =  new BaseResponse();
        try{
            AuctionGood auctionGood = auctionGoodService.get(id);
            //待审核状态的拍品才允许审核
            if(!EnumAuctionStatus.TO_AUDIT.getCode().equals(auctionGood.getAuctionStatus())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("待审核状态的拍品才允许审核");
                return response;
            }
            //校验拍品的结束时间是否早于当前时间
            if(!auctionGood.getEndTime().after(new Date())){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("拍品的结束时间不允许早于当前时间");
                return response;
            }
            auctionGood.setAuctionStatus(auditStatus);
            auctionGoodService.edit(auctionGood);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("审核拍品信息成功");
        }catch (Exception e){
            log.error("审核拍品信息异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("审核拍品信息失败");
        }
        return response;
    }

}
